<?php

namespace app\api\controller\web;

use app\admin\model\apply\Confg;
use app\admin\model\apply\Video;
use app\admin\model\guidance\Guidance;
use app\admin\model\guidance\Pics;
use app\admin\model\memberpoint\Integral;
use app\admin\model\nursery\Companyattestation;
use app\admin\model\nursery\NurserySell;
use app\admin\model\nursery\NurseryWantbuyOffer;
use app\admin\model\nursery\Specs;
use app\admin\model\nursery\Wantbuy;
use app\admin\model\project\Project;
use app\admin\model\userauth\Operatelog;
use app\api\controller\server\Server;
use app\common\controller\Api;
use app\common\helpers\ImageUtil;
use app\common\model\Attachment;
use app\common\model\ScoreLog;
use app\common\model\User;
use fast\Tree;
use think\Db;
use think\Log;
use think\Model;
use think\Validate;


class My extends Api
{
    protected $noNeedLogin = ['historyAgreement','course'];
    protected $noNeedRight = '*';
    protected $model ='';
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 个人历史项目列表
     */
    public function historyProjectList()
    {
        $user = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量
        $status = $this->request->param('status');//项目状态
        $where['is_del']=0;
        $where['user_id']=$user['user_id'];
        if($status){
            $where['status'] = $status;
        }
        $model = (new Project())->field('id,project_name,status,project_no,upload_time,project_logo')
                ->where($where)
                ->order('upload_time desc')
                ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);

        $total= $model->total();
        $list =  $model->items();
        if($list){
            foreach ($list as $v){
                $v['project_logo'] = ImageUtil::handle($v['project_logo']);
            }
        }
        $result = array("total" =>$total , "list" => $list);
        $this->success("获取成功",$result);;
    }

    /**
     * @throws \think\exception\DbException
     * 操作日志
     */
    public function userOperateLog()
    {
        $user = $this->auth->getUserinfo();
        $page = $this->request->param('page',1);//页码
        $pageSize = $this->request->param('pageSize',10);//每页展示数量

        $model = (new Operatelog())->alias('ol')
            ->field('project_name,ol.createtime,project_progress_id,describe,ol.status')
            ->join('fa_project p','ol.project_id = p.id','left')
            ->where(['ol.user_id'=>$user['id']])
            ->order('ol.createtime desc')
            ->paginate($pageSize, '', ['page' => $page, 'list_rows' => $pageSize]);

        $total= $model->total();
        $list =  $model->items();

        $data =[];
        if($list){
            foreach ($list as $v){

                $ms = '';
                if($v['describe'] == '查看'){
                    $ms='对《'.$v['project_name'].'》详情查看';
                }
                if($v['describe'] == '新增'){
                    $ms='新增了《'.$v['project_name'].'》';
                }
                if($v['describe'] == '修改'){
                    $ms='对《'.$v['project_name'].'》内容修改';
                }
                if($v['describe'] == '删除'){
                    $ms='对《'.$v['project_name'].'》进行删除';
                }
                if($v['project_progress_id']){
                    $ms='对《'.$v['project_name'].'》进程进行了'.$v['describe'];
                }

                $data[]=[
                    'title'=>'对《'.$v['project_name'].'》进行操作',
                    'ms'=>$ms,
                    'status'=>$v['status'],
                    'createtime'=>$v['createtime'],
                    'day'=>date('m-d',strtotime($v['createtime'])),
                ];
            }

        }
        $result = array("total" =>$total , "list" => $data);
        $this->success("获取成功",$result);;
    }


    /**
     * @throws \think\exception\DbException
     * 项目保密协议
     */
    public function historyAgreement()
    {
        $info = Guidance::get(1);
        $this->success("获取成功",$info['agreement']);;
    }

    /**
     * @throws \think\exception\DbException
     * 用户教程
     */
    public function course()
    {
        $info = Guidance::get(1);

        $pics = (new Pics())->field('img')->order('sort desc')->select();
        if($pics){
            foreach ($pics as  $v){
                $v['img']= ImageUtil::handle($v['img']);
            }
        }
        $this->success("获取成功",[
            'video'=>ImageUtil::handle($info['video']),
            'pics'=>$pics
        ]);;
    }

}
